package com.redis.delay;

import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author lizhilong
 * @Date 2020/12/4 19:38
 */
@Slf4j
public class RedisDelayedQueue {

    @Resource
    private RedissonClient redissonClient;

    public <T> void addQueue(T t, Long delay, TimeUnit timeUnit, String queueName) {
        log.info("添加队列：{}，延时：{}，单位：{}", queueName, delay, timeUnit);
        RBlockingDeque<T> blockingDeque = redissonClient.getBlockingDeque(queueName);
        RDelayedQueue<T> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
        delayedQueue.offer(t, delay, timeUnit);
    }
}
